package com.shijiu.controller;

import com.shijiu.annotation.RepeatSubmit;
import com.shijiu.utils.CosUploadUtils;
import com.shijiu.common.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author xlj
 * @Date Created in  2023/3/18 14:30
 * @DESCRIPTION:
 * @Version V1.0
 */
@RestController
@RequestMapping("upload")
@Transactional(rollbackFor = Exception.class)
public class UploadController {

    @Autowired
    private CosUploadUtils cosUploadUtils;

    @ApiOperation("文件上传")
    @RepeatSubmit(interval = 2000, message = "接口请求过于频繁")
    @PostMapping
    public Result upload(MultipartFile file) {
        boolean upload = cosUploadUtils.upload(file);
        if (upload){
            return Result.success(CosUploadUtils.url + file.getOriginalFilename());
        }
        return Result.fail(20001,"上传失败");
    }
}
